﻿using System.Collections.Generic;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using WindowModules.Actions;

namespace WindowModules.Wpf
{
    /// <summary>
    /// Interaction logic for WindowMove.xaml
    /// </summary>
    public partial class WindowSendKeys : UserControl, IWindowActionConfigControl
    {
        private readonly WindowActionSendKeys action;
        private readonly InterceptKeys interceptKeys;

        private readonly List<System.Windows.Forms.Keys> keysDown;

        public WindowSendKeys(WindowActionSendKeys action)
        {
            InitializeComponent();

            string currKey = textBlockKeys.Text = action.KeyList.ToArray()[0].ToString();

            switch (currKey)
            {
                case "V":
                    textBlockKeys.Text = "Deck1 Play/Pause";
                    break;
                case "C":
                    textBlockKeys.Text = "Deck1 Sync";
                    break;
                case "G":
                    textBlockKeys.Text = "Deck2 Play/Pause";
                    break;
                case "B":
                    textBlockKeys.Text = "Deck2 Sync";
                    break;
                case "Q":
                    textBlockKeys.Text = "Deck1 Highs Down";
                    break;
                case "A":
                    textBlockKeys.Text = "Deck1 Mids Down";
                    break;
                case "Z":
                    textBlockKeys.Text = "Deck1 Bass Down";
                    break;
                case "W":
                    textBlockKeys.Text = "Deck1 Highs Up";
                    break;
                case "S":
                    textBlockKeys.Text = "Deck1 Mids Up";
                    break;
                case "X":
                    textBlockKeys.Text = "Deck1 Bass Up";
                    break;
                case "I":
                    textBlockKeys.Text = "Deck2 Highs Down";
                    break;
                case "J":
                    textBlockKeys.Text = "Deck2 Mids Down";
                    break;
                case "N":
                    textBlockKeys.Text = "Deck2 Bass Down";
                    break;
                case "O":
                    textBlockKeys.Text = "Deck2 Highs Up";
                    break;
                case "K":
                    textBlockKeys.Text = "Deck2 Mids Up";
                    break;
                case "M":
                    textBlockKeys.Text = "Deck2 Bass Up";
                    break;
                case "D": textBlockKeys.Text = "Deck1 Vol Down"; break;
                case "E": textBlockKeys.Text = "Deck1 Vol Up"; break;
                case "H": textBlockKeys.Text = "Deck2 Vol Down"; break;
                case "U": textBlockKeys.Text = "Deck2 Vol Up"; break;
                case "D7": textBlockKeys.Text = "Master Vol Down"; break;
                case "D8": textBlockKeys.Text = "Master Vol Up"; break;

                case "D1": textBlockKeys.Text = "Deck1 Cue 1"; break;
                case "D2": textBlockKeys.Text = "Deck1 Cue 2"; break;
                case "D3": textBlockKeys.Text = "Deck1 Cue 3"; break;

                case "D4": textBlockKeys.Text = "Deck2 Cue 1"; break;
                case "D5": textBlockKeys.Text = "Deck2 Cue 2"; break;
                case "D6": textBlockKeys.Text = "Deck2 Cue 3"; break;

                case "Left": textBlockKeys.Text = "Cross Left"; break;
                case "Right": textBlockKeys.Text = "Cross Right"; break;
                case "Down": textBlockKeys.Text = "Mix Next"; break;
                default:
                    return;
            }

            //textBlockKeys.Text = action.KeyList.ToArray()[0].ToString();

            this.action = action;

            interceptKeys = InterceptKeys.Instance;

            keysDown = new List<System.Windows.Forms.Keys>();
            foreach (System.Windows.Forms.Keys key in action.KeyList)
            {
                keysDown.Add(key);
            }

            //UpdateText();
        }

        private void interceptKeys_OnKeyDown(System.Windows.Forms.Keys key)
        {
            if (key == System.Windows.Forms.Keys.LControlKey || key == System.Windows.Forms.Keys.RControlKey)
                key = System.Windows.Forms.Keys.Control;
            else if (key == System.Windows.Forms.Keys.LMenu || key == System.Windows.Forms.Keys.RMenu || key == System.Windows.Forms.Keys.Menu)
                key = System.Windows.Forms.Keys.Alt;
            else if (key == System.Windows.Forms.Keys.LShiftKey || key == System.Windows.Forms.Keys.RShiftKey || key == System.Windows.Forms.Keys.ShiftKey)
                key = System.Windows.Forms.Keys.Shift;
            if (!keysDown.Contains(key))
            {
                if (key == System.Windows.Forms.Keys.Control || key == System.Windows.Forms.Keys.Shift || key == System.Windows.Forms.Keys.Alt)
                    keysDown.Insert(0, key);
                else
                    keysDown.Add(key);

                UpdateText();
            }
        }

        private void UpdateText()
        {
            string str = string.Empty;
            foreach (System.Windows.Forms.Keys key in keysDown)
            {
                if (str == string.Empty)
                {
                    str = key.ToString();
                }
                else
                {
                    str += " + " + key.ToString();
                }
            }

            textBlockKeys.Text = str;
        }

        public void Save()
        {
            action.KeyList = keysDown;
            interceptKeys.RemoveHook();
        }

        private void buttonClear_Click(object sender, RoutedEventArgs e)
        {
            keysDown.Clear();
            UpdateText();
        }

        private void textBlockKeys_GotFocus(object sender, RoutedEventArgs e)
        {
            interceptKeys.OnKeyDown += interceptKeys_OnKeyDown;
            interceptKeys.SetHook();
        }

        private void textBlockKeys_LostFocus(object sender, RoutedEventArgs e)
        {
            interceptKeys.RemoveHook();
            interceptKeys.OnKeyDown -= interceptKeys_OnKeyDown;
        }

        private void UserControl_LostFocus(object sender, RoutedEventArgs e)
        {
            textBlockKeys_LostFocus(sender, e);
        }

        private void textBlockKeys_KeyDown(object sender, KeyEventArgs e)
        {
            e.Handled = true;
        }
    }
}
